package jeye.client;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Vector;

import jeye.client.exception.HttpServiceException;
import jeye.client.util.HttpClientConnector;
import jeye.client.util.LineReader;
import jeye.client.util.URLBuilder;

public class HttpServiceClient {

	private HttpClientConnector connector;

	public HttpServiceClient(HttpClientConnector connector) {
		this.connector = connector;
	}

	public Vector getEvents() throws HttpServiceException {
		try {
			Vector result = new Vector();
			LineReader reader = new LineReader(connector.getInputStream());

			String line = reader.readLine();

			while (line != null) {
				result.addElement(new Long(Long.parseLong(line)));
				line = reader.readLine();
			}

			return result;
			
		} catch (Exception e) {
			throw new HttpServiceException("Error during retrieving events: " + e);
		}
	}

	public Vector getEventFrames(Long eventId) throws HttpServiceException {
		try {
			Vector result = new Vector();
			URLBuilder url = connector.getConnectionURL();

			url.addParameter("type", "frames");
			url.addParameter("id", eventId);

			LineReader reader = new LineReader(connector.getInputStream(url));

			String line = reader.readLine();

			while (line != null) {
				result.addElement(new Long(Long.parseLong(line)));

				line = reader.readLine();
			}

			return result;
		} catch (Exception e) {
			throw new HttpServiceException(
					"Error during retrieving event frames: " + e);
		}
	}

	public InputStream getEventFrame(Long frameId) throws HttpServiceException {
		return getEventFrame(frameId, null, null, null);
	}

	public InputStream getEventFrame(Long frameId, Integer width, Integer height)
			throws HttpServiceException {
		return getEventFrame(frameId, width, height, null);
	}

	public InputStream getFrameFromStream(InputStream inputStream)
			throws HttpServiceException {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			boolean endLineAchieved = false;

			while (!endLineAchieved) {
				int buffer = inputStream.read();

				if ((char) buffer == '\n') {
					endLineAchieved = true;
				} else {
					baos.write(buffer);
				}
			}

			long count = Long.parseLong(baos.toString());

			baos.reset();

			for (long i = 0; i < count; i++) {
				int buffer = inputStream.read();
				baos.write(buffer);
			}

			return new ByteArrayInputStream(baos.toByteArray());
		} catch (Exception e) {
			throw new HttpServiceException(
					"Error during retrieving event frames: " + e);
		}
	}

	public InputStream getEventFrameSequence(Long eventId, Integer width, Integer height, Float quality) throws HttpServiceException {
		try {
			URLBuilder url = connector.getConnectionURL();

			// TODO wykorzystac stale
			url.addParameter("action", "sequenceAction");
			url.addParameter("eventId", eventId);

			if (width != null && height != null) {
				url.addParameter("width", width);
				url.addParameter("height", height);
			}

			if (quality != null) {
				url.addParameter("quality", quality);
			}
			
			System.out.println("URL: " + url.toString());
			
			return connector.getInputStream(url);
			
		} catch (Exception e) {
			throw new HttpServiceException("Error during retrieving frame: " + e);
		}
	}

	/**
	 * @deprecated
	 */
	public InputStream getEventFrame(Long frameId, Integer width,
			Integer height, Float quality) throws HttpServiceException {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			URLBuilder url = connector.getConnectionURL();

			// TODO wykorzystac stale
			url.addParameter("action", "frameAction");
			url.addParameter("id", frameId);

			if (width != null && height != null) {
				url.addParameter("width", width);
				url.addParameter("height", height);
			}

			if (quality != null) {
				url.addParameter("quality", quality);
			}

			InputStream is = connector.getInputStream(url);

			int b = is.read();

			while (b > -1) {
				baos.write(b);
				b = is.read();
			}

			is.close();

			return new ByteArrayInputStream(baos.toByteArray());
		} catch (Exception e) {
			throw new HttpServiceException("Error during retrieving frame: "
					+ e);
		}
	}

}
